/*
 * Team Name: Team Sasquatch
 * Cody Sharff
 * Anthony Rios
 * Prince Kishore
 * Travis Drake
 */

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;

import model.Auction;
import model.Condition;
import model.Item;
import model.User;
import model.UserType;

/**
 * This class displays a GUI that contains information
 * about a specific auction.
 * 
 * @author Cody Sharff
 *
 */
public class AuctionViewGUI extends Observable {
	
	/**
	 * Panel for displaying auction information.
	 */
	private final JPanel my_auction_panel;
	
	/**
	 * User object to determine if it is a bidder, AC Employee,
	 * or NPO that is viewing the information.
	 */
	private final User my_user;
	
	/**
	 * The auction that is being displayed.
	 */
	private final Auction my_auction;
	
	/**
	 * Auction information to be displayed in the panel.
	 */
	private final ArrayList<String> my_fields;
	
	/**
	 * A button that allows the user to view more info about an
	 * item.
	 */
	private final JButton my_item_view_button = new JButton("View Item Info");
	
	/**
	 * A button that allows an NPO/AC Employee to edit the
	 * auction currently being displayed.
	 */
	private final JButton my_edit_button = new JButton("Edit");
	
	/**
	 * A button that allows the user to return to the previous
	 * screen.
	 */
	private final JButton my_back_button = new JButton("Back");
	
	/**
	 * A button that allows the AuctionCentral employee or NPO to add
	 * an item to an auction.
	 */
	private final JButton my_add_item_button = new JButton("Add Item");
	
	/**
	 * TBD.
	 */
	private final JScrollPane my_scroll_pane = new JScrollPane();
	
	/**
	 * A list that contains the auction's items.
	 */
	private final JList my_list;
	
	/**
	 * JPanel main window title
	 */
    public static final String WINDOW_TITLE = "Auction Information";
    
    /**
     * Constant string that is used to notify the master GUI
     * to return to the previous GUI.
     */
    public static final String BACK = "Back";
    
    /**
     * Constant string that is used to notify the master GUI
     * to add an auction.
     */
    public static final String ADD_AUCTION = "Add Auction";
    
    /**
     * Format the Calendar object in the form of 
     */
    private static final DateFormat date_format = new SimpleDateFormat("EEE MMM dd");
    
    /**
     * Format the Calendar object in the form of time of day.
     */
    private static final DateFormat time_format = new SimpleDateFormat("HH:mm");
    
    /**
     * Static auction information labels
     */
    private static final String[] my_auction_info = {"NPO: ", "Contact Person: ", "Contact's Phone: ", "AuctionCentral Intake Person: ",
    	"Auction Date: ", "Auction Start Time: ", "Auction Duration: ",  "Comments: ", "List of Items: "};
	
    /**
     * Used to format the text.
     */
	private final Font my_font = new Font("Arial", Font.BOLD, 16);
	
	/**
	 * Constructs a AuctionViewGUI object that generates a panel that displays
	 * information about a particular auction. This is observed by MasterGUI
	 * to determine when to close this view.
	 */
    public AuctionViewGUI(Auction the_auction, User the_user) {
    	my_user = the_user;
    	my_auction = the_auction;
    	my_auction_panel = new JPanel(new BorderLayout());
    	my_auction_panel.setName("Auction Information");
     	my_fields = new ArrayList<String>(Arrays.asList(the_auction.getNPO(), the_auction.getContactPerson(), String.valueOf(the_auction.getContactNumber()), the_auction.getACIntake(),
    			date_format.format(the_auction.getDate().getTime()), time_format.format(the_auction.getDate().getTime()), Double.toString((double) the_auction.getDuration() / 60) + " hour(s)", the_auction.getComments()));
    	my_list = new JList(the_auction.getItems().toArray());
     	addListeners();
     	layoutComponents();
    }

    /**
     * Attaches action listeners to the buttons.
     */
	@SuppressWarnings("serial")
	private void addListeners() {
	    my_back_button.addActionListener(new AbstractAction() {
	        public void actionPerformed(final ActionEvent the_event) {
	          setChanged();
	          notifyObservers("Back");
	        }
	      });
	    my_item_view_button.addActionListener(new AbstractAction() {
	        public void actionPerformed(final ActionEvent the_event) {
	        	setChanged();
	            notifyObservers(my_list.getSelectedValue()); //should return Item object
	        }
	      });
	    my_add_item_button.addActionListener(new AbstractAction() {
	        public void actionPerformed(final ActionEvent the_event) {
	        	setChanged();
	            notifyObservers(my_auction); //should return Item object
	        }
	      });
	    
	    
	}

	/**
	 * Configures the layout and configuration of the labels, buttons and
	 * layouts for the panel.
	 */
	private void layoutComponents() {
		final JPanel spring_layout = new JPanel();
		spring_layout.setLayout(new SpringLayout());
		int i = 0;
        for (i = 0; i < my_auction_info.length-1; i++) {
            spring_layout.add(new JLabel(my_auction_info[i], JLabel.TRAILING));
            spring_layout.add(new JLabel(my_fields.get(i)));
        }
        spring_layout.add(new JLabel(my_auction_info[i], JLabel.TRAILING));
        spring_layout.add(my_list);
        
      //Lay out the panel.
        SpringUtilities.makeCompactGrid(spring_layout,
                                        my_auction_info.length, 2, //rows, cols
                                        190, 135,        //initX, initY
                                        8, 8);       //xPad, yPad
        
        my_auction_panel.add(spring_layout, BorderLayout.NORTH);
        JPanel south_panel = new JPanel(new FlowLayout());
        south_panel.add(my_back_button);
        if (my_user.getType().compareTo(UserType.AUCTION_CENTRAL) == 0 || my_user.getName().compareTo(my_auction.getNPO()) == 0) {
        	south_panel.add(my_edit_button);
        	south_panel.add(my_add_item_button);
        }
        south_panel.add(my_item_view_button);
        my_auction_panel.add(south_panel, BorderLayout.SOUTH);
    }
    
	/**
	 * Returns the panel.
	 */
    public JPanel getPanel() {
    	return my_auction_panel;
    }
}
